// pages/index/index.js
const db = wx.cloud.database();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    on: true,
    a: '',
    b: '',
    currentIndex: 0,
    hideNotice: false,
    /* 轮播图  'cloud://cloud1-7g1a0u3je2bf6f8d.636c-cloud1-7g1a0u3je2bf6f8d-1314056523/a.png',*/
/*     background: ['cloud://cloud1-7g1a0u3je2bf6f8d.636c-cloud1-7g1a0u3je2bf6f8d-1314056523/20191118023412_kwzUN.jpeg', 'cloud://cloud1-7g1a0u3je2bf6f8d.636c-cloud1-7g1a0u3je2bf6f8d-1314056523/20191118023411_jUREK.jpeg', 'cloud://cloud1-7g1a0u3je2bf6f8d.636c-cloud1-7g1a0u3je2bf6f8d-1314056523/20191118023412_8TE4n.jpeg'], */
    background: ['cloud://cloud1-7g1a0u3je2bf6f8d.636c-cloud1-7g1a0u3je2bf6f8d-1314056523/轮播图/Snipaste_2022-10-30_14-34-35.png', 'cloud://cloud1-7g1a0u3je2bf6f8d.636c-cloud1-7g1a0u3je2bf6f8d-1314056523/轮播图/海报1_1000x420.png', 'cloud://cloud1-7g1a0u3je2bf6f8d.636c-cloud1-7g1a0u3je2bf6f8d-1314056523/轮播图/宣传招聘互联网商务公众号首图__2022-10-30+11_21_11_990x420.png'],
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    interval: 4000,
    duration: 1000,

    cardCur: 0,
    DotStyle: true,
    /* 引用组件 轮播图 */
    /*   swiperList: [{
              id: 0,
              type: 'image',
              url: 'cloud://cloud1-6geteyha69be255a.636c-cloud1-6geteyha69be255a-1308694316/bEhCWEliZk1tenkwUmNMRWlVaVhLN0x0ajBmVzE2WWJ2MHBwOHdBWGhMTT0.jpg'
            }, {
              id: 1,
                type: 'image',
                url: 'cloud://cloud1-6geteyha69be255a.636c-cloud1-6geteyha69be255a-1308694316/bEhCWEliZk1tenpIbzdwR3dhS0NyVWlDZElMQ2hvaXpHVHUxSTBvYTZOOD0_1920x1070.jpg',
            }, {
              id: 2,
              type: 'image',
              url: 'cloud://cloud1-6geteyha69be255a.636c-cloud1-6geteyha69be255a-1308694316/loveimages/16614190126160'
            }, {
              id: 3,
              type: 'image',
              url: 'cloud://cloud1-6geteyha69be255a.636c-cloud1-6geteyha69be255a-1308694316/bEhCWEliZk1tenkwUmNMRWlVaVhLd2tPM0t5R3c2NlErQzlFS1J5Tktyaz0_1920x1070.jpg'
            }, {
              id: 4,
              type: 'image',
              url: 'cloud://cloud1-6geteyha69be255a.636c-cloud1-6geteyha69be255a-1308694316/loveimages/16613004773050'
            }, {
              id: 5,
              type: 'image',
              url: 'cloud://cloud1-6geteyha69be255a.636c-cloud1-6geteyha69be255a-1308694316/loveimages/16613004773050'
            },  ],  */



    indexConfig: [{
        icon: "/imgs/paotui.png",
        text: "快递代取",
        url: '/pages/kuaidi/kuaidi',
      },
      {
        icon: "/imgs/run.png",
        text: "校园跑腿",
        url: '/pages/run/run'
      },
      {
        icon: "/imgs/waimai.png",
        text: "外卖到寝",
        url: '/pages/waimai/waimai'
      },
      {
        icon: "/imgs/shitang.png",
        text: "食堂买饭",
        url: '/pages/shitang/shitang'
      },
      {
        icon: "/imgs/laji.png",
        text: "代搬东西",
        url: '/pages/laji/laji'
      },
      {
        icon: "/imgs/qitq.png",
        text: "其他服务",
        url: '/pages/qita/qita'
      },
      {
        icon: "/imgs/jiaji.png",
        text: "加急单",
        url: '/pages/jiaji/jiaji'
      },
      {
        icon: "/imgs/xuzhi.png",
        text: "跑腿须知",
        url: '/pages/know/know'
      },


    ],
    LBTs: "",
    gonggao: "",


  },
  // cardSwiper
  /*       cardSwiper(e) {
          this.setData({
            cardCur: e.detail.current
          })
        }, */
  DotStyle(e) {
    this.setData({
      DotStyle: e.detail.value
    })
  },

  //轮播图去index
  bindchange(res) {
    //console.log('轮播图',res.detail.source)
    if (res.detail.source == 'autoplay' || res.detail.source == 'touch') { // 防止快速切换造成的卡顿
      this.setData({
        currentIndex: res.detail.current
      })
    }

  },
  on() {
    this.setData({
      on: false
    })
  },

  // 跳转到接单页面
  gotojiedan() {
    wx.navigateTo({
      url: '/pages/applyOrder/applyOrder',
    })

  },


  //点击联系我,并复制出来  公告
  get() {
    wx.showModal({
      confirmColor: '#B452CD', //确定的颜色
      cancelColor: '#9C9C9C', //取消的颜色 
      showCancel: true,
      title: '公告',
      content: '申请接单或源码获取加我微信。如有纠纷请联系我-微信：YXHLYF19115314436',
      success(res) {
        if (res.confirm) {
          wx.setClipboardData({
            data: 'YXHLYF19115314436',
            success: function (res) {
              wx.showToast({
                title: '微信号已复制',
                //icon:"none",
                image: "/imgs/提交成功.png", //只能是本地的
                mask: "true" //是否设置点击蒙版，防止点击穿透
              })
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  /* 新闻公告 跳转 */
  to11() {
    wx.navigateTo({
      url: '/pages/new/new',
    })
  },
  /* 随机音乐 跳转*/
  to22() {
    wx.navigateTo({
      url: '/pages/music/music',
    })
  },
  /* 天气 跳转*/
  to33() {
    wx.navigateTo({
      url: '/pages/tianqi/tianqi',
    })
  },
  /* 快递 跳转*/
  /*    to44() {
      wx.navigateTo({
        url: '/pages/kuaidi/kuaidi',
      })
    }, */
  // 判断登录，是则过，否则跳转登录
  /*   to1(e) {
      console.log(e)
      const userInfo = wx.getStorageSync('key');
      const url = e.currentTarget.dataset.url
      if (userInfo) {
        wx.navigateTo({
          url,
        })
      } else {
        wx.showToast({
          icon: "error",
          title: '请先去登录 ！',
        })

        setTimeout(() => {  
          wx.reLaunch({
            url: '/pages/my/my'
          })
        }, 500)



      }
    },
   */
  toDetail(e) {
    console.log(e)
    const url = e.currentTarget.dataset.url;
    const userInfo = wx.getStorageSync('key');
    if (userInfo) {
      wx.navigateTo({
        url,
      })
    } else {
      wx.showToast({
        icon: "error",
        title: '请先去登录 ！',
      })
      setTimeout(() => { // 延时跳转
        wx.reLaunch({
          url: '/pages/my/my'
        })
      }, 500)
    }

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    /*     wx.showLoading({
          title: '加载中...',
        })
        const that = this
        db.collection("ershou").get()
          .then(res => {
            console.log("全部数据为：", res)
            that.setData({
              thins: res.data,
            })
          })
        setTimeout(() => {
          wx.hideLoading()
        }, 800) */


    // 过审核
    /*     const that = this
        db.collection("a").get()
          .then(res => {
              console.log("11111111",res.data[0].a)
            that.setData({
              a: res.data[0].a,
            })
          }) */
    //this.towerSwiper('swiperList');
    // 广告
    // 在页面中定义插屏广告
    let interstitialAd = null
    // 在页面onLoad回调事件中创建插屏广告实例
    if (wx.createInterstitialAd) {
      interstitialAd = wx.createInterstitialAd({
        adUnitId: 'adunit-909671c7acc8c025'
      })
      interstitialAd.onLoad(() => {})
      interstitialAd.onError((err) => {})
      interstitialAd.onClose(() => {})
    }
    // 在适合的场景显示插屏广告
    if (interstitialAd) {
      interstitialAd.show().catch((err) => {
        console.error(err)
      })
    };


    db.collection("LBT").get()
          .then(res => {
              console.log("轮播图：",res.data)
              this.setData({
                LBTs: res.data,
              })
          }) 

          db.collection("gonggao").get()
          .then(res => {
              console.log("公告文字为：",res.data)
              this.setData({
                gonggao: res.data,
              })
          }) 




  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  toq() {
    const userInfo = wx.getStorageSync('key');
    if (userInfo) {
      wx.navigateTo({
        url: '/pages/kuaidi/kuaidi',
      })
    } else {
      wx.showToast({
        icon: "error",
        title: '请先去登录 ！',
      })
      setTimeout(() => { // 延时跳转
        wx.reLaunch({
          url: '/pages/my/my'
        })
      }, 500)
    }


  },
  tor() {
    const userInfo = wx.getStorageSync('key');
    if (userInfo) {
      wx.navigateTo({
        url: '/pages/run/run',
      })
    } else {
      wx.showToast({
        icon: "error",
        title: '请先去登录 ！',
      })
      setTimeout(() => { // 延时跳转
        wx.reLaunch({
          url: '/pages/my/my'
        })
      }, 500)
    }

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0
      })
    }
  },



  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showToast({
      title: '页面刷新中...',
      icon: "none"
    })
  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {


  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: "移通校园跑腿儿"
    }
  },

  // 跳转到 表白墙
  loveto() {
    const userInfo = wx.getStorageSync('key');
    if (userInfo) {
      wx.navigateTo({
        url: '/pages/love/love',
      })
    } else {
      wx.showToast({
        icon: "error",
        title: '请先去登录 ！',
      })
      setTimeout(() => { // 延时跳转
        wx.reLaunch({
          url: '/pages/my/my'
        })
      }, 500)
    }
  },
  // 失物招领 与 二手交易 跳转详情页
  todetail1() {
    const userInfo = wx.getStorageSync('key');
    if (userInfo) {
      wx.navigateTo({
        url: '/pages/lose/lose',
      })
    } else {
      wx.showToast({
        icon: "error",
        title: '请先去登录 ！',
      })
      setTimeout(() => { // 延时跳转
        wx.reLaunch({
          url: '/pages/my/my'
        })
      }, 500)
    }
  },
  // 二手交易
  todetail2() {
    const userInfo = wx.getStorageSync('key');
    if (userInfo) {
      wx.navigateTo({
        url: '/pages/details/details',
      })
    } else {
      wx.showToast({
        icon: "error",
        title: '请先去登录 ！',
      })
      setTimeout(() => { // 延时跳转
        wx.reLaunch({
          url: '/pages/my/my'
        })
      }, 500)
    }
  },

  /* 分享到朋友圈 */
  onShareTimeline: function (item) {
    return {
      title: '校园跑腿',
      imageUrl: 'https://c-ssl.duitang.com/uploads/blog/202107/28/20210728003227_fc99b.jpg'
    }
  },


  // towerSwiper
  // 初始化towerSwiper
  towerSwiper(name) {
    let list = this.data[name];
    for (let i = 0; i < list.length; i++) {
      list[i].zIndex = parseInt(list.length / 2) + 1 - Math.abs(i - parseInt(list.length / 2))
      list[i].mLeft = i - parseInt(list.length / 2)
    }
    this.setData({
      swiperList: list
    })
  },

  /*   towerStart(e) {
      this.setData({
        towerStart: e.touches[0].pageX
      })
    },
    
    towerMove(e) {
      this.setData({
        direction: e.touches[0].pageX - this.data.towerStart > 0 ? 'right' : 'left'
      })
    },
    
    towerEnd(e) {
      let direction = this.data.direction;
      let list = this.data.swiperList;
      if (direction == 'right') {
        let mLeft = list[0].mLeft;
        let zIndex = list[0].zIndex;
        for (let i = 1; i < list.length; i++) {
          list[i - 1].mLeft = list[i].mLeft
          list[i - 1].zIndex = list[i].zIndex
        }
        list[list.length - 1].mLeft = mLeft;
        list[list.length - 1].zIndex = zIndex;
        this.setData({
          swiperList: list
        })
      } else {
        let mLeft = list[list.length - 1].mLeft;
        let zIndex = list[list.length - 1].zIndex;
        for (let i = list.length - 1; i > 0; i--) {
          list[i].mLeft = list[i - 1].mLeft
          list[i].zIndex = list[i - 1].zIndex
        }
        list[0].mLeft = mLeft;
        list[0].zIndex = zIndex;
        this.setData({
          swiperList: list
        })
      }
    } */
  selectTab(e) {
    const {
      id
    } = e.currentTarget.dataset;
    this.setData({
      tabNow: id,
    })
    if (id === 0) {
      this.onLoad();
    } else if (id === 1) {
      this.gethot();
    } else if (id === 2) {
      this.getnew();
    }
  },

  // 跳转到详情 页面
  to(res) {
    const index = res.currentTarget.dataset.index
    const _id = this.data.thins[index]._id
    const imgdata = this.data.thins[index]

    wx.setStorageSync('ershouID', _id)
    wx.cloud.callFunction({
      name: "look",
      data: {
        _id,
        lookNumber: this.data.thins[index].lookNumber,
      },
      success: (res) => {
        //this.onLoad()
      },
      fail: (res) => {
        showToast({
          icon: 'none',
          title: '服务器出错，请重试...'
        })
      }
    })

    const dataobj = {
      lookNumber: imgdata.lookNumber,
      beizhu: imgdata.beizhu,
      createTime: imgdata.createTime,
      imgs: imgdata.imgs,
      phonenumber: imgdata.phonenumber,
      newold: imgdata.newold,
      sell: imgdata.sell,
      state: imgdata.state,
      time: imgdata.time,
      title: imgdata.title,
      two: imgdata.two,
      userInfo: imgdata.userInfo,
      type: imgdata.type,
      _id: imgdata._id,
      _openid: imgdata._openid,
    }
    //跳转
    wx.navigateTo({
      url: '/pages/detailsS/detailsS',
      success: (res) => {
        res.eventChannel.emit('acceptDataFromOpenerPage', {
          data: dataobj
        })
      }
    })
  },






})